package com.example.user.util;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @description: JedisPool工具类
 * 加载配置文件，配置连接池的参数
 * 提供获取连接的方法
 * @author: Nick 2020/12/3 10:01 下午
 **/

public class JedisPoolUtil {
    private static volatile JedisPool jedisPool;

    //懒汉单例模式
/*    public  static JedisPoolUtil getJedisPoolUtil(){
        if(jedisPoolUtil==null){
            jedisPoolUtil = new JedisPoolUtil();
        };
        return jedisPoolUtil;
    }*/

    private JedisPoolUtil() {
    }

    public static JedisPool getJedisPoolInstance() {
        if (null == jedisPool) {
            synchronized (JedisPoolUtil.class) {
                if (null == jedisPool) {
                    JedisPoolConfig poolConfig = new JedisPoolConfig();
                    poolConfig.setMaxTotal(200);
                    poolConfig.setMaxIdle(32);
                    poolConfig.setMaxWaitMillis(100*1000);
                    poolConfig.setBlockWhenExhausted(true);
                    poolConfig.setTestOnBorrow(true);  // ping  PONG

                    jedisPool = new JedisPool(poolConfig, "192.168.100.131", 6379, 60000,"333" );
                }
            }
        }
        return jedisPool;
    }

    /*static {
        //读取配置文件
        InputStream is = JedisPoolUtil.class.getClassLoader().getResourceAsStream("jedis.properties");
        //创建Properties对象
        Properties properties = new Properties();
        //关联文件
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取数据,设置到JedisPoolConfig中
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(properties.getProperty("MaxTotal")));
        config.setMaxIdle(Integer.parseInt(properties.getProperty("MaxIdle")));

        //初始化JedisPool
        jedisPool = new JedisPool(config,properties.getProperty("host"), Integer.parseInt(properties.getProperty("port")));
    }*/
    /*
    获取连接的方法
     */
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }

//    public static void release(JedisPool jedisPool, Jedis jedis) {
//        if (null != jedis) {
//            jedisPool.returnResource(jedis);
//        }
//    }
}


